/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dao;




import java.sql.*;
import java.sql.Date;
import java.sql.Savepoint;
import java.text.SimpleDateFormat;
import com.mysql.jdbc.CallableStatement;
import com.mysql.jdbc.Connection;
import pojo.PhieuNhapPOJO;

/**
 *
 * @author HP
 */
public class PhieuNhapDAO {
    MySqlDataAccessHelper helper=new MySqlDataAccessHelper();

/**
 * Ham Them Phieu Nhap Tra ve MaPhieuNhap Vua Them
 * @param pnh
 * @return
 */
public int  ThemPhieuNhap(PhieuNhapPOJO pnh)
{

     int maphieunhap=0;
    try{
           String result;
           SimpleDateFormat format;
           format=new SimpleDateFormat("yyyy-dd-MM");
           result=format.format(pnh.getNgayNhap());
           Date today=Date.valueOf(result);
        //Buoc 1: Khoi tao va tao ket noi
        Connection cn=null;
        //Buoc 2: Mo kert noi
        helper.open();
        cn= (Connection) helper.getConnection();
        cn.setAutoCommit(false);
       // Statement state=(Statement) cn.createStatement();
        Savepoint save=cn.setSavepoint();
        //Buoc 3: Thuc thi cau truy van
        CallableStatement statement=(CallableStatement) cn.prepareCall("{call sp_ThemPhieuNhap(?,?)}");
        statement.setDate(1, today);


        statement.registerOutParameter(2,Types.INTEGER);
        //int n=-1;
        statement.execute();
        boolean kq;
        ResultSet rs=statement.getResultSet();
        if(rs.next())
        {
            maphieunhap=statement.getInt(2);
            cn.commit();
        }
        else
        {
            cn.rollback(save);
        }
    }
    catch(Exception ex)
    {

    }
    helper.close();
    return maphieunhap;
}
// Ham lay MAX ma hoa don
 public  Integer LayMaxPhieuNhap()
  {

      Integer kq=0;
      try
      {
             MySqlDataAccessHelper helper=new MySqlDataAccessHelper();
            helper.open();
            Connection cn=(Connection) helper.getConnection();
            CallableStatement statement =(CallableStatement) cn.prepareCall("{call sp_LayMaxPhieuNhap()}");
            statement.execute();
            ResultSet rs = statement.getResultSet();
           rs.next();
           kq=rs.getInt(1);
            helper.close();
            return kq;
         }
      catch(Exception ex){
          System.out.print(ex.getMessage());
          return kq;
      }
    }

 public int ThemPhieuNhap_sql(PhieuNhapPOJO pn)
{
    boolean kq=false;
    int mapn=0;
    try{
           String result;
           SimpleDateFormat format;
           format=new SimpleDateFormat("yyyy-dd-MM");
           result=format.format(pn.getNgayNhap());
           Date today=Date.valueOf(result);
        //Buoc 1: Khoi tao va tao ket noi
        Connection cn=null;
        //Buoc 2: Mo kert noi
        helper.open();
        cn= (Connection) helper.getConnection();
        cn.setAutoCommit(false);
        Savepoint save=cn.setSavepoint();
        //Buoc 3: Thuc thi cau truy van
        String sql1="INSERT INTO phieunhap(NgayNhap)VALUES('"+ result+"')";
        Statement stm= (Statement) cn.createStatement();
        //pre.setDate(1, today);
      //  pre.setFloat(2,hd.getTongTien());
        int n=-1;
        n=stm.executeUpdate(sql1,Statement.RETURN_GENERATED_KEYS);

        if(n>0)
        {
            mapn=1;
            //ResultSet rs=stm.getGeneratedKeys();
           // mapn=rs.getInt(1);
            cn.commit();
            kq=true;
        }
        else
        {
            cn.rollback(save);
        }
    }
    catch(Exception ex)
    {

    }
    helper.close();
    return mapn;
}
}

